package cn.bdqn.pn.controller.user;

import cn.bdqn.pn.pojo.user.Admin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.AbstractDocument;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/Rouser")
public class RouserController {
    // 访问登录页
    @RequestMapping("/login")
    public String login() {
        // 下句语句会抛出异常交由GlobalExceptionHandler类的errorHandler方法处理
        // System.out.println(1 / 0);

        return "login";
    }

    // 登录处理
   /* @RequestMapping("/doLogin")
    public String doLogin(String username, String password,String codes_text, HttpServletRequest request, Model model) {
        HttpSession session = WebUtils.toHttp(request).getSession();
        // 使用Shiro编写认证处理
        // 1、获取Subject
        Subject subject = SecurityUtils.getSubject();

        // 2、封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 3、执行登录
        try {
            // 登录成功
            subject.login(token);
            session.setAttribute("admin",token.getUsername());
            // 返回当前用户的帐号
            model.addAttribute("currentuser", username);

            return "shops_index";
        } catch (UnknownAccountException exception) {
            // 返回错误信息
            model.addAttribute("msg", "账号错误！");

            return "login";
        } catch (IncorrectCredentialsException exception) {
            // 返回错误信息
            model.addAttribute("msg", "密码错误！");

            return "login";
        }
    }*/

    // 注销处理
    @RequestMapping("/doLogout")
    public String doLogout() {
        // 1、获取Subject
        Subject subject = SecurityUtils.getSubject();

        // 2、执行注销
        try {
            subject.logout();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return "login";
        }
    }
}